package com.wjw.controller;

import com.wjw.entity.Category;
import com.wjw.entity.Product;
import com.wjw.entity.ProductPicture;
import com.wjw.service.CategoryService;
import com.wjw.service.ProductPictureService;
import com.wjw.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (product)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductPictureService productPictureService;

    /**
     * @return
     */
    @RequestMapping(value = "/getCategory")
    public Map getCategory() {
        List<Category> category = categoryService.getCategory();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", "001");
        map.put("category", category);
        return map;
    }

    /**
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "/getAllProduct")
    public Map getAllProduct(@RequestBody Map<String, Object> paramMap) {
        List<Product> product = productService.getAllProduct();
        HashMap<String, Object> map = new HashMap<>();
        map.put("Product", product);
        map.put("total", product.size());
        return map;
    }

    /**
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "/getProductByCategory")
    public Map getProductByCategory(@RequestBody Map<String, Object> paramMap) {
        ArrayList categoryIDList = (ArrayList) paramMap.get("categoryID");
        int categoryID = (int) categoryIDList.get(0);
        int currentPage = (int) paramMap.get("currentPage");
        int pageSize = (int) paramMap.get("pageSize");
        List<Product> product = productService.getProductByCategory(categoryID, currentPage, pageSize);
        HashMap<String, Object> map = new HashMap<>();
        map.put("Product", product);
        return map;
    }

    /**
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "/getPromoProduct")
    public Map getPromoProduct(@RequestBody Map<String, String> paramMap) {
        String categoryName = paramMap.get("categoryName");
        List<Product> product = productService.getProductByCategoryName(categoryName);
        HashMap<String, Object> map = new HashMap<>();
        map.put("Product", product);
        return map;
    }

    /**
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "/getHotProduct")
    public Map getHotProduct(@RequestBody Map<String, List<String>> paramMap) {
        List<String> categoryName = paramMap.get("categoryName");
        List<Product> product = new ArrayList<>();
        for (String name : categoryName) {
            List<Product> tempProduct = productService.getProductByCategoryName(name);
            product.addAll(tempProduct);   //添加多个类型产品
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("Product", product);
        return map;
    }

    /**
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "/getDetails")
    public Map getDetails(@RequestBody Map<String, Integer> paramMap) {
        int productID = paramMap.get("productID");
        Product product = productService.getProductByID(productID);
        HashMap<String, Object> map = new HashMap<>();
        map.put("Product", new ArrayList<Product>() {{
            this.add(product);
        }});
        return map;
    }

    /**
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "/getDetailsPicture")
    public Map getDetailsPicture(@RequestBody Map<String, Integer> paramMap) {
        int productID = paramMap.get("productID");
        List<ProductPicture> ProductPicture = productPictureService.getDetailsPicture(productID);
        HashMap<String, Object> map = new HashMap<>();
        map.put("ProductPicture", ProductPicture);
        return map;
    }

    @RequestMapping(value = "/getProductBySearch")
    public Map getProductBySearch(@RequestBody Map<String, Object> paramMap) {
        String search = (String) paramMap.get("search");
        int currentPage = (int) paramMap.get("currentPage");
        int pageSize = (int) paramMap.get("pageSize");
        int offset = (currentPage - 1) * pageSize;
        List<Category> categorys = categoryService.getCategory();
        HashMap<String, Object> map = new HashMap<>();
        for (Category category : categorys) {
            if (category.getCategoryName().equals(search)) {
                List<Product> product = productService.getProductByCategory(category.getCategoryId(), offset, pageSize);
                map.put("code", "001");
                map.put("Product", product);
                map.put("total", product.size());
                return map;
            }
        }
        List<Product> product = productService.getProductBySearch(search);
        map.put("code", "001");
        map.put("Product", product);
        map.put("total", product.size());
        return map;
    }

    @RequestMapping(value = "/add")
    public Boolean add(Product product) {
        return productService.add(product);
    }


}
